﻿package ben 
{
	import caurina.transitions.Tweener;
	
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	
	/**
	* ...
	* @author Ben
	*/
	public class ScrollBar extends MovieClip
	{
		
		// ELEMENTS DE LA SCROLL BAR
		public var fondScroll_mc:MovieClip
		//public var ScrollArrowUp_mc:MovieClip
		//public var ScrollArrowDown_mc:MovieClip
		public var Scroll_mc:MovieClip
		public var ArrowUp_mc:MovieClip
		public var ArrowDown_mc:MovieClip
		
		// CLIP SCROLLÉ
		public var clipScroll:DisplayObject
		public var clipMask:DisplayObject
		
		public function ScrollBar() 
		{
			Scroll_mc.buttonMode = true;
		}
		
		public function init(theTarget:DisplayObject, theMask:DisplayObject)
		{
			// INITIALISATION
			clipScroll = theTarget;
			clipMask = theMask;
			enabled = false;
			//alpha = 0;
						
			arrowDim = 18;// ScrollArrowUp_mc.width;
			
			// MOLETTE SOURIS
			stage.addEventListener(MouseEvent.MOUSE_WHEEL, onMouseWheelEvent);
			
			// EVENT 
			Scroll_mc.addEventListener(Event.ENTER_FRAME, scrollOef);
			
			// ON REPLACE LES ELEMENTS
			replace();
			
			// EVENT SUR LE SCROLL
			Scroll_mc.addEventListener(MouseEvent.MOUSE_DOWN, scrollPress);
			stage.addEventListener(MouseEvent.MOUSE_UP, scrollRelease);		
		}
		
		public function replace(e:Event = null)
		{
			if (!clipMask && !clipScroll)
			{
				return
			}
			x = clipMask.x + clipMask.width;
			y = clipMask.y;
			fondScroll_mc.height = clipMask.height;
			//ScrollArrowDown_mc.y = fondScroll_mc.height;
			Scroll_mc.y = 0;// ScrollArrowUp_mc.y + ScrollArrowUp_mc.height + 1;
		}
		
		private var scrolling:Number = 0
		private var scrollModif:Boolean = true
		private var arrowDim:Number = 0;
		
		private function onMouseWheelEvent(e:MouseEvent)
		{			
			if (enabled)
			{
				scrollModif = true;
				scrolling -= e.delta * 20;
			}
		}
		
		private function scrollOef(e:Event)
		{			
			var diff:Number = (clipScroll.height+clipScroll.height/100) - clipMask.height;
			
			if (diff <= 0) 
			{
				if (enabled) 
				{
					enabled = false;
					var newY:Number = 0;
					Tweener.addTween(this,{ alpha:0, time:1});
				}else 
				{
					var newY:Number = 0;
				}
			} 
			else 
			{
				if (!enabled) 
				{
					enabled = true;
					Tweener.addTween(this,{ alpha:100, time:1});	
				}
				
				Scroll_mc.height = Math.max (arrowDim*3, Math.round ((fondScroll_mc.height /*- (arrowDim + 1) * 2*/) / ((clipScroll.height+30) / clipMask.height)));
				//Scroll_mc.height = 200;
				
				if (scrollModif) 
				{
					scrolling = Math.min (fondScroll_mc.height - Scroll_mc.height , Math.max (0, scrolling));
					Scroll_mc.y = scrolling;
				}
				else
				{
					scrolling = Scroll_mc.y;
				}
				
				var endScroll:Number = fondScroll_mc.height - Scroll_mc.height;
				var b:Number = diff / endScroll;
				var newY:Number = Math.min(0,(-diff / endScroll) * Scroll_mc.y + b + y);
			}
			
			clipScroll.y -= Math.round(clipScroll.y - newY );
			
			ArrowUp_mc.y = Scroll_mc.y + 8;
			ArrowDown_mc.y = Scroll_mc.y + Scroll_mc.height - 8;
		}
		
		private function scrollPress(e:MouseEvent)
		{
			scrollModif = false;
			var DragTarget:Rectangle = new Rectangle (fondScroll_mc.x, fondScroll_mc.y , 0, fondScroll_mc.height - Scroll_mc.height);
			Scroll_mc.startDrag(false, DragTarget );
		}
		
		private function scrollRelease(e:MouseEvent)
		{
			scrollModif = true;
			Scroll_mc.stopDrag ();
		}
		
		
		
		

	

	
// ARROWS
/*ScrollArrowUp_mc.onPress = function ()
{
	scrollModif = true;
	this.onEnterFrame = function ()
	{
		scrolling -= 10;
	};
};
ScrollArrowDown_mc.onPress = function ()
{
	scrollModif = true;
	this.onEnterFrame = function ()
	{
		scrolling += 10;
	};
};
ScrollArrowUp_mc.onRelease = ScrollArrowUp_mc.onReleaseOutside = ScrollArrowDown_mc.onRelease = ScrollArrowDown_mc.onReleaseOutside = function ()
{
	delete this.onEnterFrame;
};*/

		
		
	}
	
}
